const app = getApp()
import { getShopInfo, getSupplierInfo, followSupplier } from '../../../api/index'

Page({
  data: {
    supplierId: '',
    detail: {},
    contactList: [],
    indicatorDots: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    activeIndex: 0,
    bgColor: '#f5f5f5'
  },
  switchCard (e) {
    let current = e.detail.current
    this.setData({ activeIndex: current })
  },
  async collectionSupplier () {
    try {
      await followSupplier({ supplierId: this.data.detail.supplierInfo.id })
      wx.showToast({ title: '操作成功！', icon: 'none' })
      this.data.detail.supplierInfo.follow = !this.data.detail.supplierInfo.follow
      this.setData({ detail: this.data.detail })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getSupplierInfo () {
    try {
      const { ResultSet } = await getSupplierInfo({ supplierId: this.data.supplierId })
      this.setData({ contactList: ResultSet.userData.contactList })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getDetail () {
    try {
      const { ResultSet } = await getShopInfo({ supplierId: this.data.supplierId })
      wx.setNavigationBarTitle({ title: ResultSet.userData.supplierInfo.companyName })
      this.setData({ detail: ResultSet.userData })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad (query) {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    if (query.scene) {
      const scene = decodeURIComponent(query.scene)
      let id = scene.split('=')[1]
      this.setData({ supplierId: id, safeBottom: screenHeight - bottom })
    } else {
      this.setData({ supplierId: query.id, safeBottom: screenHeight - bottom })
      // wx.setNavigationBarTitle({ title: query.name })
    }
    this.getSupplierInfo()
    this.getDetail()
  }
})